package common

import (
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/base/user/mp"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/base/user/user_service"
	"git.myscrm.cn/golang/common/ykerrcode"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type OpenController struct {
	controller.BaseController
	mpService   mp.MpUserServiceIface
	userService user_service.UserSerivceIface
}

func NewOpenController(
	mpService mp.MpUserServiceIface,
	userService user_service.UserSerivceIface,
) *OpenController {
	return &OpenController{
		mpService:   mpService,
		userService: userService,
	}
}

func (o *OpenController) RegisterRouter(g *gin.RouterGroup) {
	// 获取小程序access token
	g.GET("/get-mp-access-token", o.GetMpAccessToken)
}

func (o *OpenController) GetMpAccessToken(ctx *gin.Context) {
	accessToken, err := o.mpService.GetMpAccessToken(ctx)
	if err != nil {
		stark.Logger.Error(ctx, "open GetMpAccessToken 报错", err)
		o.Fail(ctx, ykerrcode.FAIL, err.Error())
		return
	}

	o.Success(ctx, map[string]interface{}{
		"access_token": accessToken.AccessToken,
		"expire_in":    accessToken.ExpireIn})
}
